/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.querygraph.algorithm;

import com.hp.hpl.jena.graph.Node;
import edu.yonsei.iwb.smarttv.model.ConceptualGraph;
import com.hp.hpl.jena.rdf.model.Resource;
import edu.yonsei.iwb.smarttv.model.ConceptArc;
import edu.yonsei.iwb.smarttv.model.MArc;
import edu.yonsei.iwb.smarttv.model.ResourceNode;
import edu.yonsei.iwb.smarttv.querygraph.VariableMap;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author Onlyjiny
 */
public class ShortestPathVector extends Hashtable<ConceptArc, ShortestPath> {

    private int v_num;
    private double score;
    private Resource target_URI = null;
    private String spID;

    /**
     * ShortestPathVector에 포함되어 있는 모든 Resource들을 중복 없이 저장하여 반환
     *
     * @return 노드들의 집합
     */
    public Vector<Resource> getNodeSet() {
        Vector<Resource> nodeSet = new Vector<Resource>();
        Enumeration<ShortestPath> e = this.elements();
        while (e.hasMoreElements()) {
            ShortestPath sp = e.nextElement();
            for (int i = 0; i < sp.size(); i++) {

                //Resource 대신에 MArc가 꺼내짐

                MArc arc = sp.get(i);
                Resource headnode = arc.getPw().getDomain();
                Resource tailnode = arc.getPw().getRange();

//                Resource r = sp.get(i).getNode();
//                System.out.println("SPV 에 포함된 Resource    " + r); 
                if (!nodeSet.contains(headnode)) {
                    nodeSet.add(headnode);
                }
                if (!nodeSet.contains(tailnode)) {
                    nodeSet.add(tailnode);
                }
            }
        }
        return nodeSet;
    }

    public Vector<Node> getAllVariablesByClass(Resource r) {
        Vector<Node> result = new Vector<Node>();
        Enumeration<ConceptArc> e = this.keys();
        while (e.hasMoreElements()) {
            ConceptArc ca = e.nextElement();
            ShortestPath sp = this.get(ca);
            VariableMap vm = sp.getVariableMap();
            for (int i = 0; i < sp.size(); i++) {
                MArc arc = sp.get(i);
                Node[] nodeset = vm.getVarMapByIndex().get(i);
                if(arc.getHeadNode().getNode().equals(r)){
                    if(!result.contains(nodeset[0])){
                        result.add(nodeset[0]);
                    }
                }
                if(arc.getTailNode().getNode().equals(r)){
                    if(!result.contains(nodeset[1])){
                        result.add(nodeset[1]);
                    }
                }
            }
        }
        return result;
    }

    public ConceptualGraph getConceptualSpanningTree() {

        ConceptualGraph cst = new ConceptualGraph();
        Enumeration<ConceptArc> e = this.keys();
        while (e.hasMoreElements()) {
            ConceptArc ca = e.nextElement();
            cst.add(ca);
        }

        return cst;
    }

    /**
     * 매개변수인 Spanning Tree를 구성하는 ConceptArc와 일치하는 ShortestPath의 집합을 반환
     *
     * @param st Spanning Tree
     * @return
     */
    public ShortestPathVector getConceptArcMatchedSPV(ConceptualGraph st) {
        ShortestPathVector spv = new ShortestPathVector();
        for (int i = 0; i < st.size(); i++) {
            ShortestPath sp = this.get(st.get(i));
            if (sp != null) {
                spv.put(st.get(i), sp);
            }
        }
        return spv;
    }

    public ShortestPathVector getNodeMatchedSPV(Resource r) {
        ShortestPathVector spv = new ShortestPathVector();
        Enumeration<ConceptArc> e = this.keys();
        while (e.hasMoreElements()) {
            ConceptArc ca = e.nextElement();
            if (ca.contains(r)) {
                spv.put(ca, this.get(ca));
            }
        }
        return spv;
    }
    
    
    /**
     * ShortestPath의 변수 정의
     * @param v_num 
     */
    public void setVariablesOfShortestPath(int v_num) {
        this.v_num = v_num;
        Enumeration<ConceptArc> e = this.keys();
        while (e.hasMoreElements()) {
            ConceptArc ca = e.nextElement();
            ShortestPath sp = this.get(ca);
            VariableMap variableMap = new VariableMap(sp, ca, this.v_num);
            sp.setVariableMap(variableMap);
            this.v_num = variableMap.getVariableSeq();
        }
    }

    public int getVariableSeq() {
        return this.v_num;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public double getScore() {
        return this.score;
    }

    public void setTargetURI(Resource target) {
        this.target_URI = target;
    }

    public Resource getTargetURI() {
        return this.target_URI;
    }

    public void updateShortestPath(ShortestPathVector spvold) {
        Enumeration<ConceptArc> e = spvold.keys();
        while (e.hasMoreElements()) {
            ConceptArc ca = e.nextElement();
            ShortestPath sp = spvold.get(ca);
            this.put(ca, sp);
        }
    }

    public void setSPID(String spID) {
        this.spID = spID;
    }

    public String getSPID() {
        return this.spID;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        Enumeration<ShortestPath> e = this.elements();
        while (e.hasMoreElements()) {
            ShortestPath sp = e.nextElement();
            sb.append(sp.toString());
        }
        return sb.toString();
    }
}
